﻿Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Imports System.IO

Public Class CreateRollout
    Public MainForm As StoreGroupControl
    Dim FinalSubject As String
    Sub CollectInFolder()
        'Dim apath As String = My.Application.Info.DirectoryPath
        FinalSubject = txtsubject.Text
        FinalSubject = Regex.Replace(txtsubject.Text, "[^A-Za-z0-9\-/]", "_")
        Directory.CreateDirectory(MainForm.SelectedGroupPath & FinalSubject)
        For Each item In lstImages.Items
            Dim FileInfo_ As New FileInfo(item)
            File.Copy(FileInfo_.FullName, MainForm.SelectedGroupPath & FinalSubject & "\" & FileInfo_.Name)
        Next
        For Each item In lstdatabasefiles.Items
            Dim FileInfo_ As New FileInfo(item)
            File.Copy(FileInfo_.FullName, MainForm.SelectedGroupPath & FinalSubject & "\" & FileInfo_.Name)
        Next
        Try
            If txtremarks.Text <> "" Then
                Dim FILE_NAME As String = MainForm.SelectedGroupPath & FinalSubject & "\Message.txt"
                Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
                objWriter.WriteLine(txtremarks.Text)
                objWriter.Close()
            End If
        Catch ex As Exception

        End Try
        Try
            Dim FILE_NAME As String = MainForm.SelectedGroupPath & FinalSubject & "\Title.txt"
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
            objWriter.WriteLine(txtsubject.Text)
            objWriter.Close()
        Catch ex As Exception

        End Try
    End Sub
    Sub AddToZip()
        Try
            If File.Exists(MainForm.SelectedGroupPath & FinalSubject & ".rar") = True Then
                File.Delete(MainForm.SelectedGroupPath & FinalSubject & ".rar")
            End If
            Process1.StartInfo.Arguments = "a -ep """ & MainForm.SelectedGroupPath & FinalSubject & ".rar"" """ & MainForm.SelectedGroupPath & FinalSubject & """"
            Process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            Process1.Start()
            Process1.WaitForExit()
        Catch ex As Exception

        End Try
    End Sub
    Sub Clean()
        Try
            For Each item In Directory.GetFiles(MainForm.SelectedGroupPath & FinalSubject)
                File.Delete(item.ToString)
            Next
            Directory.Delete(MainForm.SelectedGroupPath & FinalSubject)
        Catch ex As Exception
        End Try
    End Sub
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        'Compile in 1 Directory
        If txtsubject.Text = "" Then
            MsgBox("Invalid Subject!", vbInformation)
            Return
        End If
        If lstdatabasefiles.Items.Count <= 0 Then
            MsgBox("Please Load DBF Files", vbInformation)
            Return
        End If
        CollectInFolder()
        AddToZip()
        Clean()
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
   

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim LoadImagesDialog As New OpenFileDialog
        With LoadImagesDialog
            .Filter = "(Image Files)|*.jpg;*.png;*.bmp;*.gif;"
            .Multiselect = True
            .Title = "Browse Button Print Screen"
        End With

        If LoadImagesDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each iten In LoadImagesDialog.FileNames
                lstImages.Items.Add(iten.ToString)
            Next

        End If

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim LoadImagesDialog As New OpenFileDialog
        With LoadImagesDialog
            .Filter = "Database Files|*.dbf;|All Files|*.*;"
            .Multiselect = True
            .Title = "Browse Button Print Screen"
        End With

        If LoadImagesDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each iten In LoadImagesDialog.FileNames
                lstdatabasefiles.Items.Add(iten.ToString)
            Next

        End If
    End Sub

    Private Sub CreateRollout_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class
